//@Time:2022/4/7 16:10
//@Author:aFun

package indi.csms_springboot.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import indi.csms_springboot.domain.CountResultVO;
import indi.csms_springboot.domain.Course;
import indi.csms_springboot.domain.SSFOVO;
import indi.csms_springboot.domain.StudentSelectCourseVO;
import indi.csms_springboot.service.OfficerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.Map;

@RestController
@RequestMapping("/officerController")
public class OfficerController {

    @Autowired
    private OfficerService officerService;

    // 课程设置审批模块
    @RequestMapping("/findJudgeByPage")
    public ModelAndView findJudgeByPage(HttpServletRequest request) throws UnsupportedEncodingException {
        ModelAndView modelAndView = new ModelAndView();
        request.setCharacterEncoding("utf-8");
        String currentPage = request.getParameter("currentPage");
        String rows = request.getParameter("rows");

        if (currentPage == null || "".equals(currentPage)) {
            currentPage = "1";
        }

        if (rows == null || "".equals(rows)) {
            rows = "5";
        }

        Map<String, String[]> condition = request.getParameterMap();
        IPage<Course> pb = officerService.findCourseJudgeByPage(currentPage, rows, condition);


        request.setAttribute("pb", pb);
        request.setAttribute("condition", condition);

        modelAndView.setViewName("officerFindCourse");

        return modelAndView;
    }

    @RequestMapping("/alterJudge")
    public ModelAndView alterJudge(HttpServletRequest request) throws UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        ModelAndView modelAndView=new ModelAndView();
        Map<String, String[]> maps = request.getParameterMap();
        String[] couIds = maps.get("couId");
        String[] judges = maps.get("judge");
        String[] isOpens = maps.get("isopen");
        for (int i = 0; i < couIds.length; i += 1) {
            Course c = new Course(Integer.parseInt(couIds[i]), null, -1,
                    null, Integer.parseInt(judges[i]), Integer.parseInt(isOpens[i]), -1);
            // couHour limitNum 是无参考价值的 在mapper中是不会被使用的
            boolean b1 = officerService.examineAndapprove(c);
        }
        modelAndView.setViewName("redirect:"+request.getContextPath()+"/officerController/findJudgeByPage");
        return modelAndView;
    }

    // 统计模块
    @RequestMapping("/findCountByPage")
    public ModelAndView findCountByPage(HttpServletRequest request) throws UnsupportedEncodingException {
        ModelAndView modelAndView=new ModelAndView();

        request.setCharacterEncoding("utf-8");
        String currentPage = request.getParameter("currentPage");
        String rows = request.getParameter("rows");

        if (currentPage == null || "".equals(currentPage)) {
            currentPage = "1";
        }

        if (rows == null || "".equals(rows)) {
            rows = "5";
        }

        Map<String, String[]> condition = request.getParameterMap();
        IPage<CountResultVO> pb = officerService.countResult(currentPage, rows, condition);


        request.setAttribute("pb", pb);
        request.setAttribute("condition", condition);
        modelAndView.setViewName("officerCount");

        return modelAndView;
    }


    // 选课结果管理模块
    @RequestMapping("/findCourseMessageByPage")
    public ModelAndView findCourseMessageByPage(HttpServletRequest request) throws UnsupportedEncodingException {
        ModelAndView modelAndView=new ModelAndView();
        request.setCharacterEncoding("utf-8");
        String currentPage = request.getParameter("currentPage");
        String rows = request.getParameter("rows");

        if (currentPage == null || "".equals(currentPage)) {
            currentPage = "1";
        }

        if (rows == null || "".equals(rows)) {
            rows = "5";
        }

        Map<String, String[]> condition = request.getParameterMap();
        IPage<StudentSelectCourseVO> pb = officerService.selectionResult(currentPage, rows, condition);

        request.setAttribute("pb", pb);
        request.setAttribute("condition", condition);
        modelAndView.setViewName("officerCourseSelectionResult");

        return modelAndView;
    }

    @RequestMapping("/delCourseSelectionResult")
    public ModelAndView delCourseSelectionResult(HttpServletRequest request) throws UnsupportedEncodingException {
        ModelAndView modelAndView=new ModelAndView();
        boolean b = officerService.deleteSelection(Integer.parseInt(request.getParameter("stuId")), Integer.parseInt(request.getParameter("couId")));
        modelAndView.setViewName("redirect:"+request.getContextPath()+"/officerController/findCourseMessageByPage");
        return modelAndView;
    }


    // 成绩单生成模块
    @RequestMapping("/findScoreByPage")
    public ModelAndView findScoreByPage(HttpServletRequest request) throws UnsupportedEncodingException {
        ModelAndView modelAndView=new ModelAndView();
        request.setCharacterEncoding("utf-8");
        String currentPage = request.getParameter("currentPage");
        String rows = request.getParameter("rows");

        if (currentPage == null || "".equals(currentPage)) {
            currentPage = "1";
        }

        if (rows == null || "".equals(rows)) {
            rows = "5";
        }

        Map<String, String[]> condition = request.getParameterMap();
        IPage<SSFOVO> pb = officerService.studentScore(currentPage, rows, condition);


        request.setAttribute("pb", pb);
        request.setAttribute("condition", condition);


        modelAndView.setViewName("officerScoreSelectionResult");

        return modelAndView;
    }

}
